package cn.xxstudy.visitshop.ui.activity.main.fragment.home.viewmodel

import androidx.lifecycle.MutableLiveData
import cn.xxstudy.visitshop.network.ApiService
import cn.xxstudy.visitshop.network.data.InfoData
import cn.xxstudy.visitshop.network.data.Response
import cn.xxstudy.visitshop.network.repository.BaseRepository
import cn.xxstudy.visitshop.ui.activity.main.fragment.home.model.BannerData
import cn.xxstudy.visitshop.ui.activity.task.model.TaskData

/**
 * @author ：DELL on 2021/3/24 .
 * @packages ：cn.xxstudy.visitshop.ui.activity.main.fragment.home.viewmodel .
 * TODO:主页
 */
class HomeRepository : BaseRepository() {

    /**
     * 轮播图
     */
    suspend fun banner(
        bannerLiveData: MutableLiveData<ArrayList<BannerData>>,
        failLiveData: MutableLiveData<String>
    ) {
        val request = request { ApiService.api().banner() }
        if (request is Response.SUCCESS) {
            bannerLiveData.postValue(request.data)
        }
        if (request is Response.FAILED) {
            failLiveData.postValue(request.message)
        }
    }

    /**
     * 最近TOP3任务
     */
    suspend fun taskList(
        taskLiveData: MutableLiveData<ArrayList<TaskData>>,
        failLiveData: MutableLiveData<String>
    ) {
        val request = request { ApiService.api().taskList(page_size = 3) }
        if (request is Response.SUCCESS) {
            taskLiveData.postValue(request.data.results)
        }
        if (request is Response.FAILED) {
            failLiveData.postValue(request.message)
        }
    }

    /**
     * 最近TOP3资讯
     */
    suspend fun infoList(
        infoLiveData: MutableLiveData<ArrayList<InfoData>>,
        failLiveData: MutableLiveData<String>
    ) {
        val request = request { ApiService.api().infoList(page_size = 3) }
        if (request is Response.SUCCESS) {
            infoLiveData.postValue(request.data.results)
        }
        if (request is Response.FAILED) {
            failLiveData.postValue(request.message)
        }
    }
}